package com.forum.controller;

import com.forum.bo.TipBo;
import com.forum.common.pojo.PageResult;
import com.forum.service.TipService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

/**
 * @Author Mirror
 * @CreateDate 2020/3/22.
 */
@Controller
@RequestMapping("/tip")
public class TipController {

    @Autowired
    private TipService tipService;

    /**
     * 根据条件查询你帖子
     * @param page  页数
     * @param rows  行数
     * @param tipTitle  标题
     * @param recently  最近
     * @param active    活跃
     * @param essence   精华
     * @return
     */
    @GetMapping("/tips")
    public ResponseEntity<PageInfo<TipBo>> queryTipsByCond(
            @RequestParam(name = "page", defaultValue = "1") Integer page,
            @RequestParam(name = "rows",defaultValue = "5") Integer rows,
            @RequestParam(name = "tipTitle", required = false) String tipTitle,
            @RequestParam(name = "recently",required = false) Boolean recently,
            @RequestParam(name = "active", required = false)Boolean active,
            @RequestParam(name = "essence",required = false)Boolean essence
    ){
        PageInfo<TipBo> pageInfo = this.tipService.queryTipsByCond(page,rows,tipTitle,recently,active,essence);
       if (CollectionUtils.isEmpty(pageInfo.getList())){
           //404
          return ResponseEntity.notFound().build();
       }
       //200
        return ResponseEntity.ok(pageInfo);
    }

    /**
     * 根据帖子ID查询帖子详情
     * @param tipId
     * @return
     */
    @GetMapping("{tipId}")
    public ResponseEntity<TipBo> queryTipByTipId(@PathVariable(name = "tipId") Long tipId){
        TipBo tipBo = this.tipService.queryTipByTipId(tipId);
        if (tipBo == null){
           return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(tipBo);
    }
}
